﻿/*******************************************************
 *  医院管理 相关操作js
 * <p>Title: hosRecordManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2017年12月22日 下午5:41:57
 * @version 1.0
 * *****************************************************/
//美沙酮打卡记录
var options, unitzTree,selNode,
    $table, addOrEditUrl, orgId = null, isParent = false, folder_path, issetOut = false;
var addIndex, queryType = "man";//man：按人次，num：按次数
var hosRecordManager = {
    //初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        hosRecordManager.bindZtree();
        hosRecordManager.initSelect();
        hosRecordManager.initDate();
        hosRecordManager.initTable();
        hosRecordManager.bindEvent();
        parent.delLoadBoxs();
    },
    //初始化医院下拉
    initSelect:function(){
        // 初始化医院下拉
        $.ajax({
            url: ctx + '/BaneHospital/GetHospitalDataByPer',
            type: "post",
            async: false,
            dataType: 'json',
            success: function (data) {
                if (data.Statu != 0) 
                    return false;
                var itemStr = "<option value=''>空</option>";
                $.each(data.Data, function (i, item) {
                    itemStr += "<option value='" + item.hos_id + "'>" + item.hos_name + "</option>";
                });
                $('#hos_id').append(itemStr);
                $('#hos_id').selectpicker('refresh');
            }
            //,
            //error: function () {
            //    layer.msg('数据异常~', { icon: 5 });
            //}
        });
    },
    //初始化组织机构树
    bindZtree: function () {
        var setting = {
            view: {
                showIcon: true,
                selectedMulti: false,
                fontCss: getFontCss
            },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false
            },
            async: {
                enable: true,
                dataType: "text",
                type: "post",
                url: ctx + '/SysOrg/GetOrgTreeDataByRelation',
                autoParam: ["id"]
            },
            data: {
                key: { title: "name", name: "name" },
                simpleData: { enable: true, idKey: "id", pIdKey: "pId", rootPId: 0 }
            },
            check: {
                enable: false,
                autoCheckTrigger: true,
                chkStyle: "checkbox",
                chkboxType: { "Y": "p", "N": "ps" }
            },
            callback: {
                onAsyncError: function () {
                    layer.msg("初始化单位结构树失败~", { icon: 5 });
                },
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                    //树加载完成后执行
                    var obj = unitzTree.getNodes()[0];
                    if (obj) {
                        issetOut = true;
                        unitzTree.selectNode(obj);
                        unitzTree.setting.callback.onClick(null, unitzTree.setting.treeId, obj);//模拟点击事件 
                        if (obj.children && obj.children.length > 0)
                            unitzTree.expandNode(obj);
                    }
                },
                onClick: function (event, treeId, treeNode) {
                    $('#unitTree a').css("color", "#333");
                    $('#' + treeNode.tId + '_a').css("color", "#ffffff");
                    isParent = treeNode.isParent;
                    orgId = treeNode.id;
                    selNode = treeNode;//选中节点
                    folder_path = treeNode.folder_path;
                    if (issetOut)
                    { setTimeout(function () { $table.bootstrapTable('refresh'); issetOut = false; }, 1000); }
                    else
                        $table.bootstrapTable('refresh');
                }
            }
        };
        unitzTree = $("#unitTree").initzTreeView(setting, 'ushow');
    },
    //初始化日期
    initDate:function(){
        var dateStart = {
            elem: '#dateStart',
            format: 'YYYY-MM-DD',
            max: '2099-06-16 23:59:59', //最大日期
            istime: true,
            istoday: false,
            choose: function (datas) {
                dateEnd.min = datas; //开始日选好后，重置结束日的最小日期
                dateEnd.start = datas; //将结束日的初始值设定为开始日
            }
        };
        var dateEnd = {
            elem: '#dateEnd',
            format: 'YYYY-MM-DD',
            min: laydate.now(),
            max: '2099-06-16 23:59:59',
            istime: true,
            istoday: false,
            choose: function (datas) {
                dateStart.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        laydate(dateStart);
        laydate(dateEnd);
    },
    //初始化Table
    initTable: function () {
         options = {
             url: ctx + "/BaneHospital/GetBaneRecordData",
            cutHeight: 5,
            toolbar: "#exampleToolbar",
            queryParams: function (params) {
                var hos_id = $('#hos_id').val();
                params = {
                    rows: params.limit,
                    //页面大小  
                    page: params.offset / params.limit + 1,
                    orgId: orgId,//选中单位(组织机构)
                    folder_path: folder_path,
                    hos_id: (hos_id)?hos_id:0,
                    start_date: $('#dateStart').val(),
                    end_date: $('#dateEnd').val(),
                    queryType:queryType,
                    keyword: encodeURI($('#keyword').val())//人员姓名
                }
                return params;
            },
            columns: [
	        {
	            field: '',//第一列序号
	            title: '序号',
	            align: 'center',
	            width: 50,
	            formatter: function (value, row, index) {
	                return index + 1;
	            }
	        },
	        {
	            field: 'user_name',
	            title: '人员姓名',
	            align: 'center'
	        },{
	            field: 'user_identify',
	            title: '身份证',
	            align: 'center'
	        }, {
	            field: 'hos_code',
	            title: '门诊治疗号',
	            align: 'center'
            },{
	            field: 'hos_name',
	            title: '考勤医院',
	            align: 'center'
	        }, {
	            field: 'ck_count',
	            title: '考勤次数/日期',
	            align: 'center',
	            formatter: function (value, row, index) {
	                if (queryType === "man")
	                    return "<button class='btn btn-danger btn-circle btn-outline' onclick=hosRecordManager.openRecordPage(" + row.user_id + "," + row.hos_id + ")>" + value + "</button>";
	                return row.ck_date;
	            }
	        }],
            onClickRow: function (row, $element) {

            }
        }
        $table = tableHelper.initTable("exampleTableToolbar", options);
    },
    //打开考勤详情
    openRecordPage:function(userId,hosId){
        //1.0 获取考勤时间区间 hosRecordDetial
        layer.open({
            title: ['考勤详情', 'font-size:18px;'],
            type: 2,
            content: ctx + '/BaneHospital/hosRecordDetial?hos_id=' + hosId + '&user_id=' + userId + '&dateStart=' + $('#dateStart').val() + '&dateEnd=' + $('#dateEnd').val(),
            scroll: true,//是否显示滚动条、默认不显示
            btn: '',
            area: ['1000px', '90%']
        });
    },
    //绑定默认事件
    bindEvent: function () {
        //按人次查询
        $('#btnManSearch').click(function () {
            queryType = "man";
            $table.bootstrapTable('refresh', options);
        });
        //按次数查询
        $('#btnNumSearch').on('click', function () {
            queryType = "num";
            $table.bootstrapTable('refresh', options);
        });
        //导出
        $('#hosExportExcel').on('click', function () {
            var $data = $table.bootstrapTable('getOptions');
            if (!$data || $data.data.length<=0) {
                layer.msg("没有需要导出的数据！", { icon: 5 });
                return false;
            }
            layer.msg("数据获取中...", { icon: 6, time: 9000 });
            var hos_id = $('#hos_id').val(), keyword=$('#keyword').val();
            keyword = (keyword) ? encodeURI(keyword) : "";
            hos_id = (hos_id) ? hos_id : 0;
            params = "folder_path=" + folder_path + "&hos_id=" + hos_id + "&start_date=" + $('#dateStart').val() + "&end_date=" + $('#dateEnd').val() + "&keyword=" + keyword;
            var personExport = $('#personExport');
            personExport.attr("href", ctx + "/BaneHospital/ExportRecordData?" + params);
            document.getElementById("personExport").click();
            return false;
        });
    }
}


